package linklist;

import sqlist.MyArrayListIndexOutOfException;

import java.util.List;

public class MySingleList {

    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val){
            this.val =val;
        }
    }

    public ListNode head;//单链表的头节点

    public void createList(){
        ListNode listNode1 = new ListNode(22);
        ListNode listNode2 = new ListNode(33);
        ListNode listNode3 = new ListNode(44);
        ListNode listNode4 = new ListNode(55);
        ListNode listNode5 = new ListNode(66);

        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        this.head = listNode1;
    }

    public void display(){
        ListNode cur = this.head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = head;
        this.head = node;
    }

    public void addLast(int data){
        ListNode node = new ListNode(data);
        if (head == null){
            head = node;
        }else{
            ListNode cur = head;
            while (cur.next != null){
                cur = cur.next;
            }
            cur.next = node;
        }

    }

    public void addIndex(int index,int data){
        int size = this.size();
        if(index > size || index<0){
            throw new MySingleListOutOfException("任意位置插入的时候，index不合法");
        }
        ListNode node = new ListNode(data);
        if(head ==null){
            head = node;
        }else{
            ListNode cur = head;
            for (int i = 0; i < index-1; i++) {
                cur = cur.next;
            }
            node.next = cur.next;
            cur.next = node;

        }

    }


    //关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while (cur.next != null){
            if (cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public void remove(int key) {

        if(head ==null){
            throw new MyArrayListIndexOutOfException("链表为空");
        }
        if(this.head.val ==key){
            this.head = this.head.next;
            return;
        }
        ListNode cur = head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                cur.next = cur.next.next;
                return;
            }
            cur = cur.next;
        }
        throw new MyArrayListIndexOutOfException("没有需要删除的元素");
    }

//        while (cur.next.val != key){
//            cur = cur.next;
//            if (cur.next == null){
//                break;
//            }
//        }
//        if (cur.next == null){
//            throw new MyArrayListIndexOutOfException("删除的元素超出范围");
//        }
//        cur.next = cur.next.next;
//    }

    public void removeAllKey(int key){
        ListNode prev = this.head;
        ListNode cur = this.head.next;
        while(cur.next != null){
            if(cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            }else{
                prev = prev.next;
                cur = cur.next;
            }
        }
        if(head.val == key){
            head = head.next;
        }
    }
    //链表的大小
    public int size(){
        int count = 0;
        ListNode cur = head;
        while (cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    //清空链表
    //粗暴方法
//    public void clear(){
//        this.head = null;
//    }
    public void clear(){
        ListNode cur = this.head;
        ListNode curNext = null;
        while(cur!= null){
            curNext = cur.next;
            cur.next = null;
            cur = curNext;
        }
    }

    public void reverse(){
        ListNode cur = this.head;
        ListNode pre = null;
        ListNode curNext;
        while(cur != null){
            curNext = cur.next;
            cur.next = pre;
            pre = cur;
            cur = curNext;
        }
        this.head = pre;
    }

    public ListNode midNode(){
        ListNode slow = this.head;
        ListNode fast = this.head;
        while(fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }

    public ListNode lastKNode(int k){
        if(k <= 0){
            return null;
        }
        ListNode slow = this.head;
        ListNode fast = this.head;
        for (int i = 0; i < k-1; i++) {
            fast = fast.next;
            if(fast == null){
                return null;
            }
        }
        while(fast.next != null){
            slow = slow.next;
            fast = fast.next;
        }
        return slow;
    }

    public boolean isPalindrome(){
        ListNode slow = this.head;
        ListNode fast = this.head;
        while(fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }

        ListNode cur = slow.next;
        ListNode curNext;
        while(cur != null){
            curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }

        ListNode head = this.head;
        while(head != slow){
            if(head.val != slow.val){
                return false;
            }
            if(head.next == slow){
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }
}
